<?php

namespace app\admin\controller;

use think\Db;
use think\Request;
use think\Exception;//事务处理

class Salepaytype extends Auth
{
    public function __construct(Request $request){
        $this->getChildIDdata = [];
        parent::__construct($request);
        $this->data = $request->param();
        $this->user_id = session('userid') == null ? 1 : session('userid');
        $this->lang = isset($this->data['lang']) && $this->data['lang'] ? $this->data['lang'] : is_lang();
    }

    
    public function list()
    {   
        if(isset($this->data['request']) && $this->data['request'] == 'requestAjax'){
            $join = [['multi_lang mu','zu.mkey = mu.mkey',"LEFT"]];
            // $salePayType = getSearch('sale_pay_type','zu',['zu.*,mu.content'],['mu.lang'=>$this->lang],$join,'zu.drank asc');
            if(isset($this->data['page']) && $this->data['page']){
                $page = $this->data['page'];
            }else{
                $page = 1;
            }

            if(isset($this->data['limit']) && $this->data['limit']){
                $limit = $this->data['limit'];
            }else{
                $limit = 1000000000;
            }

            $salePayType = Db::name('sale_pay_type')
                                ->alias('zu')
                                ->join($join)
                                ->field(['zu.*,mu.content'])
                                ->where(['mu.lang'=>$this->lang,'del_at'=>0])
                                ->where(['zu.del_at'=>0])
                                ->order('zu.drank asc')
                                ->page($page,$limit)
                                // ->fetchSql()
                                ->select();
            foreach ($salePayType as $k => $v) {
                $salePayType[$k]['fixx'] = $v['fix'] == 1 ? 'O' : '-';
                $salePayType[$k]['limits'] = $v['limit'] > 0 ? $v['limit'] : '-';
            }
            // dump($salePayType);die;
            $pageSum = Db::name('sale_pay_type')->where(['del_at'=>0])->count();
            return json_encode(array('status'=>1,'data'=>['data'=>$salePayType,'pageSum'=>$pageSum,'page'=>$page]));
        }else{
            return view('list');
        }
    }

    // 添加新的付款方式
    public function payTypeAdd()
    {
        if(isset($this->data['request']) && $this->data['request'] == 'insert'){
            if(isset($this->data['code']) && $this->data['code'] && isset($this->data['name']) && $this->data['name'] ){
                // 判断name是否已存在
                $has_name = hasData('multi_lang',['lang'=>$this->lang,'mkey'=>['like','%sale_pay_type_%'],'content'=>$this->data['name']]);
                if($has_name) return json_encode(array('status'=>2,'msg'=>$this->data['name'].' '.lang('already_exists')));
                // 判断code是否已存在
                $has_code = hasData('sale_pay_type',['code'=>$this->data['code']]);
                if($has_code) return json_encode(array('status'=>2,'msg'=>$this->data['code'].' '.lang('already_exists')));

                $insertData['code'] = $this->data['code'];
                $insertData['limit'] = $this->data['limit'];
                $insertData['drank'] = $this->data['sort'];
                // 是否需要重新排序
                if(hasData('sale_pay_type',['drank'=>$this->data['sort']])){
                    $is_drank = changeDrank(100,'sale_pay_type',$this->data['sort']);
                }

                Db::startTrans();
                $insertDataId = insertData('sale_pay_type',$insertData);
                if($insertDataId){
                    $res = updateData('sale_pay_type',['id'=>$insertDataId],['mkey'=>'sale_pay_type_'.$insertDataId]);
                    if($res){
                        $ress = insertMuitl('sale_pay_type_'.$insertDataId,$this->data['name']);
                        if($ress){
                            Db::commit();
                            return json_encode(array('status'=>1,'msg'=>$this->data['name'].' '.lang('added_successfully')));
                        }else{
                            Db::rollback();
                            return json_encode(array('status'=>0,'msg'=>$this->data['name'].' '.lang('add_failed')));
                        }
                    }else{
                        Db::rollback();
                        return json_encode(array('status'=>0,'msg'=>$this->data['name'].' '.lang('add_failed')));
                    }
                }else{
                    Db::rollback();
                    return json_encode(array('status'=>0,'msg'=>$this->data['name'].' '.lang('add_failed')));
                }
            }else{
                return json_encode(array('status'=>2,'msg'=>lang('improve_submission_data')));
            }
        }else{
            return view('paytypeopen',['title'=>'添加新的付款方式','requestType'=>'add']);
        }
    }


    // 编辑付款方式
    public function payTypeEdit()
    {
        if(isset($this->data['request']) && $this->data['request'] == 'edit'){
            if(isset($this->data['id']) && $this->data['id']){
                $join = [['multi_lang mu','zu.mkey = mu.mkey',"LEFT"]];
                $salePayType = getSearch('sale_pay_type','zu',['zu.*,zu.drank olddrank,mu.content'],['zu.id'=>$this->data['id'],'mu.lang'=>$this->lang],$join,'zu.drank asc');
                // dump($salePayType[0]);die;
                return json_encode(array('status'=>1,'data'=>$salePayType[0]));
            }else{
                return json_encode(array('status'=>2,'msg'=>'请求数据有误'));
            }
        /************分隔符**************/
        }else if(isset($this->data['request']) && $this->data['request'] == 'update'){
            if(isset($this->data['code']) && $this->data['code'] && isset($this->data['name']) && $this->data['name'] ){
                // 判断name是否已存在
                $has_name = Db::name('multi_lang')->where(['lang'=>$this->lang,'mkey'=>['like','%sale_pay_type%'],'content'=>$this->data['name']])->where(['mkey'=>['neq','sale_pay_type_'.$this->data['id']]])->select();
                if($has_name) return json_encode(array('status'=>2,'msg'=>$this->data['name'].' '.lang('already_exists')));
                // 判断code是否已存在
                $has_code = hasData('sale_pay_type',['code'=>$this->data['code'],'id'=>['neq',$this->data['id']]]);
                if($has_code) return json_encode(array('status'=>2,'msg'=>$this->data['code'].' '.lang('already_exists')));

                $updateData['code'] = $this->data['code'];
                $updateData['limit'] = $this->data['limit'];
                $updateData['drank'] = $this->data['sort'];

                // 是否需要从新排序
                if(hasData('sale_pay_type',['drank'=>$this->data['sort']])){
                    if($this->data['sort'] != $this->data['olddrank']){
                        if($this->data['sort'] != $this->data['olddrank']){
                            $is_drank = changeDrank(100,'sale_pay_type',$this->data['sort'],$this->data['olddrank']);
                        }
                    }
                }
                Db::startTrans();
                $updateDataId = updateData('sale_pay_type',['id'=>$this->data['id']],$updateData);
                if($updateDataId >= 0){
                        $res_m = updateData('multi_lang',['mkey'=>'sale_pay_type_'.$this->data['id'],'lang'=>$this->lang],['content'=>$this->data['name']]);
                        if($res_m >=0){
                            Db::commit();
                            return json_encode(array('status'=>1,'msg'=>$this->data['name'].' '.lang('edited_successfully')));
                        }else{
                            Db::rollback();
                            return json_encode(array('status'=>0,'msg'=>$this->data['name'].' '.lang('edit_failed')));
                        }
                }else{
                    Db::rollback();
                    return json_encode(array('status'=>0,'msg'=>$this->data['name'].' '.lang('edit_failed')));
                }
            }else{
                Db::rollback();
                return json_encode(array('status'=>0,'msg'=>$this->data['name'].' '.lang('edit_failed')));
            }
        }else{
            return view('paytypeopen',['title'=>'编辑付款方式','requestType'=>'edit','id'=>$this->data['id']]);
        }
    }

    // 软删除
    function deleteData()
    {
        if(isset($this->data['id']) && $this->data['id'])
        {
            $res = updateData('sale_pay_type',['id'=>$this->data['id']],['del_id'=>$this->user_id,'del_at'=>time()]);
            $salePayTypeName = getValue('multi_lang',['mkey'=>'sale_pay_type_'.$this->data['id'],'lang'=>$this->lang],'content');
            if($res)
                return json_encode(array('status'=>1,'msg'=>$salePayTypeName.' '.lang('deleted_successfully')));
            else
                return json_encode(array('status'=>0,'msg'=>$salePayTypeName.' '.lang('failed_to_delete')));
        }
    }

}